package org.wdcode.web.util;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.wdcode.common.constants.StringConstants;
import org.wdcode.common.util.CommonUtil;
import org.wdcode.web.constants.HttpConstants;

/**
 * Request一些相关操作类
 * @author WD
 * @since JDK6
 * @version 1.0 2010-01-20
 */
public final class RequestUtil {
	/**
	 * 获得request的提交参数 如果没有返回""
	 * @param request ServletRequest
	 * @param key 属性值
	 * @return value
	 */
	public final static String getParameter(ServletRequest request, String key) {
		return getParameter(request, key, StringConstants.EMPTY);
	}

	/**
	 * 获得request的提交参数 如果没有返回defaultValue
	 * @param request ServletRequest
	 * @param key 属性值
	 * @param defaultValue 默认值
	 * @return value
	 */
	public final static String getParameter(ServletRequest request, String key, String defaultValue) {
		// 获得值
		String value = isEmpty(request) ? defaultValue : request.getParameter(key);
		// 如果值为空 返回默认值
		return CommonUtil.isEmpty(value) ? defaultValue : value;
	}

	/**
	 * 获得request的属性 如果没有返回defaultValue
	 * @param request ServletRequest
	 * @param key 属性值
	 * @return value
	 */
	public final static Object getAttribute(ServletRequest request, String key) {
		return getAttribute(request, key, null);
	}

	/**
	 * 获得request的属性 如果没有返回defaultValue
	 * @param request ServletRequest
	 * @param key 属性值
	 * @param defaultValue 默认值
	 * @return value
	 */
	@SuppressWarnings("unchecked")
	public final static <E> E getAttribute(ServletRequest request, String key, E defaultValue) {
		// 获得值
		E value = isEmpty(request) ? defaultValue : (E) request.getAttribute(key);
		// 如果值为空 返回默认值
		return CommonUtil.isEmpty(value) ? defaultValue : value;
	}

	/**
	 * 获得Session
	 * @param request HttpServletRequest
	 * @return HttpSession
	 */
	public final static HttpSession getSession(HttpServletRequest request) {
		return isEmpty(request) ? null : request.getSession();
	}

	/**
	 * 获得Session
	 * @param request HttpServletRequest
	 * @param b Boolean
	 * @return HttpSession
	 */
	public final static HttpSession getSession(HttpServletRequest request, boolean b) {
		return isEmpty(request) ? null : request.getSession(b);
	}

	/**
	 * 设置request的属性
	 * @param request ServletRequest
	 * @param key 属性值
	 * @param value 属性值
	 */
	public final static void setAttribute(ServletRequest request, String key, Object value) {
		if (!isEmpty(request)) {
			request.setAttribute(key, value);
		}
	}

	/**
	 * 获得客户连接IP
	 * @param request Request
	 * @return 客户连接IP
	 */
	public final static String getIP(HttpServletRequest request) {
		// 判断不为空
		if (!isEmpty(request)) {
			// 获得IP
			String ip = request.getHeader(HttpConstants.HEADER_IP_X_FORWARDED_FOR);
			// 判断如果为空继续获得
			if (CommonUtil.isEmpty(ip)) {
				// 为空换方法获得
				ip = request.getHeader(HttpConstants.HEADER_IP_X_REAL_IP);
			}
			// 判断如果为空继续获得
			if (CommonUtil.isEmpty(ip)) {
				// 为空换方法获得
				ip = request.getRemoteAddr();
			}
			// 返回IP
			return ip;
		}
		// 返回""
		return StringConstants.EMPTY;
	}

	/**
	 * 判断request是否为空
	 * @param request ServletRequest
	 * @return boolean
	 */
	public final static boolean isEmpty(ServletRequest request) {
		return CommonUtil.isEmpty(request);
	}

	/**
	 * 私有构造
	 */
	private RequestUtil() {}
}
